package com.tenement.action;

import com.tenement.domain.TenementStreet;
import com.tenement.domain.TenementUser;
import com.tenement.service.StreetService;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : StreetAction  //类名
 * @Description : 跟进action  //描述
 * @Author : 易嵘             //作者
 * @Date: 2020-06-07 10:50   //时间
 */
@Controller
@RequestMapping("streetAction")
public class StreetAction {

    /**
     * 跟进服务接口
     */
    @Resource
    StreetService streetService;

    @ResponseBody
    @RequestMapping("selectAll")
    public List<TenementStreet> selectAll(String sectorId) {
        return streetService.selectAll(sectorId);
    }

    @ResponseBody
    @RequestMapping("findAll")
    public List<TenementStreet> findAll(TenementStreet street) {

        List<TenementStreet> list = streetService.findAll(street);
        //Long overCount = streetService.overCount(street, startDate, endDate);

        return list;

    }

    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Message addOrUpdate(TenementStreet street, HttpSession session, HttpServletRequest request) {
        int row;
        String ip=request.getRemoteAddr();
        TenementUser user = (TenementUser) session.getAttribute("user");
        if (StringUtils.isEmpty(street.getStreetId())) {
            row = streetService.add(street, user,ip);
        } else {
            row = streetService.update(street, user,ip);
        }

        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public Message deleteById(String id,HttpServletRequest request) {
        HttpSession session=request.getSession();
        TenementUser user= (TenementUser) session.getAttribute("user");
        String ip=request.getRemoteAddr();
        int row = streetService.deleteById(id,user,ip);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]", required = false) String ids[],HttpServletRequest request) {
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        String ip=request.getRemoteAddr();
        int row = streetService.deleteAll(ids,user,ip);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("findById")
    public TenementStreet findById(String id) {
        return streetService.findById(id);
    }

    /**
     * 日期格式化
     *
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/
}
